import { controller, httpGet as get, httpPost as post } from "inversify-express-utils";
import { UserServices } from "./user.services";
import { inject } from 'inversify'
import { Request, Response } from "express";

/**
 * ## User 模块控制器
 * - 根路径：/user
 */

@controller('/user')
export class User {

  constructor(
    @inject(UserServices) private readonly user: UserServices
  ) { }

  /**
   * ## 请求示例
   * - 请求方式：GET
   * - 请求路径：/user/get
   */
  @get('/get')
  public async login(req: Request, res: Response) {
    console.log(req.user.id) // 获取用户信息
    let result = await this.user.get()
    res.cc('ok', { data: result }, 200)
  }

  /**
   * ## 请求示例
   * - 请求方式：POST
   * - 请求路径：/user/post
   */
  @post('/post')
  public async signup(req: Request, res: Response) {
    let result = await this.user.post(req.body)
    res.cc('ok', { data: result }, 200)
  }
}

/** 扩充 Request 与 User 类型 */
declare global {
  namespace Express {
    export interface Request {
      user?: User;
    }

    export interface User {
      id: number;
      username: string;
      password: string;
    }
  }
}